<?php
/**
 * 车辆事故相关接口请求参数验证器
 * Created by PhpStorm.
 * User: LRChang
 * Date: 2018/5/30
 * Time: 11:27
 * Mail: hjklrchang@qq.com
 */

namespace app\adminapi\validate;

use app\base\validate\BaseValidate;

class CarAccidentValidate extends BaseValidate
{
    protected $rule = [
        'page' => 'isPositiveInt',
        'pagenum' => 'isPositiveInt',
//        'fields' => '',
        'where_json' => 'isWhereJson',

        'accident_id' => 'require|isPositiveInt',
        'company_id' => 'require|isPositiveInt',
        'company_name' => 'require|max:100',
        'car_id' => 'require|isPositiveInt',
        'plate_no' => 'require|max:50',
        'driver_id' => 'require|isPositiveInt',
        'driver_name' => 'require|max:20',
        'license_code' => 'require|max:30',
        'accident_time' => 'require|dateFormat:Y-m-d H:i:s',
        'address' => 'require|max:255',
        'desc' => 'max:255',
        'price' => '>=:0',
        'pic_list' => 'checkPicListJson',
        'handle_desc' => 'max:255',
        'handle_result' => 'max:255',
        'responsibility' => 'max:255',
        'remark' => 'max:255',
        'solve_person' => 'max:20',

        'accident_ids' => 'require|checkIDs',
    ];

    // 默认值
    protected $default = [
        'page' => 1,
        'pagenum' => 20,
        'where_json' => '',
        'fields' => '*',
    ];

    protected $message = [
        'page.isPositiveInt' => '页数只能为正整数',
        'pagenum.isPositiveInt' => '每页数量只能为正整数',
        'fields.isNotEmpty' => '字段不能为空',
        'where_json.isWhereJson' => '不是合法的json字符串',
        'pic_list.checkPicListJson' => '不为空时必须为json数组字符串，每个子元素必须为对象，并且仅包含"pic_name"和"pic_path"两个属性，pic_path的值不能为空',
        'accident_ids.checkIDs' => "accident_id 必须为正整数，多个请用','号连接"
    ];

    protected $scene = [
        'list' => ['page' ,'pagenum', 'fields', 'where_json'],
        'add' => [
            'company_id',
            'car_id',
            'driver_id',
            'accident_time',
            'address',
            'desc',
            'price',
            'pic_list',
            'handle_desc',
            'handle_result',
            'responsibility',
            'remark',
            'solve_person',
        ],
        'edit' => [
            'accident_id',
            'company_id',
            'car_id',
            'driver_id',
            'accident_time',
            'address',
            'desc',
            'price',
            'pic_list',
            'handle_desc',
            'handle_result',
            'responsibility',
            'remark',
            'solve_person',
        ],
        'delete' => [ 'accident_ids' ],
    ];

    /**
     * 验证图片json数组
     * @param $value
     * @return bool
     */
    protected function checkPicListJson($value){
        if(!$this->isJsonString($value)) return false;
        $value = json_decode($value, true);

        // 当传入空值时，替换为空数组
        if(empty($value)){
            $this->replaceData('pic_list', []);
            return true;
        }

        if(!is_array($value)) return false;

        foreach ($value as $arr){
            if(!is_array($arr) || count($arr) !== 2){
                return false;
            }

            foreach($arr as $k => $v){
                if(!in_array($k, ['pic_name', 'pic_path']) || !is_string($v)) return false;
                if($k === 'pic_path' && !trim($v)) return false;
            }
        }

        $this->replaceData('pic_list', $value);
        return true;
    }
}